<template>
  <div>
    <div>
      <div class="top">
        <i class="iconfont icon-xiangzuojiantou" @click="goto"></i>
        <span>当前城市 - {{ '北京' }}</span>
      </div>
      <div class="seach">
        <i class="iconfont icon-fangdajing"></i>
        <input type="text" name="" id="" placeholder="请输入城市名称" />
      </div>
    </div>
    <div class="hot">
      <div class="hotCity">热门城市</div>
      <div class="ccc" v-for="item in hotcity" :key="item">{{ item }}</div>
    </div>
    <van-index-bar :index-list="indexList">
      <div v-for="(item, key) in cityList" :key="key" class="ab">
        <van-index-anchor>{{ key }}</van-index-anchor>
        <van-cell v-for="v in item" :key="v.cityId" :title="v.name" />
      </div>
    </van-index-bar>
  </div>
</template>

<script>
import Vue from 'vue'
import { IndexBar, IndexAnchor, Cell } from 'vant'
// 导入api - 获取请求地址
import api from '@/api'

Vue.use(IndexBar)
Vue.use(IndexAnchor)
Vue.use(Cell)
export default {
  data() {
    return {
      indexList: [], // 放右边的列表
      // 定义26个字母组成的数组
      words: [],
      hotcity: [],
      city: {},
      // 定义数据
      cityList: {} // {A:[{a开头的城市1},{a开头的城市1}]}
    }
  },
  //回退
  methods: {
    goto(hash) {
      history.go(-1)
    }
  },
  // 请求数据
  created() {
    // 定义26个字母
    for (let i = 65; i <= 90; i++) {
      this.words.push(String.fromCharCode(i))
    }
    this.$http.get(api.getCity).then((res) => {
      console.log(res.data.cities)

      // 最终需要从这个里面筛选数据
      // 从26个里面筛选需要的，放在indexList中
      // 循环words
      this.words.forEach((word) => {
        // 遍历res.data.cities
        res.data.cities.forEach((item) => {
          if (item.pinyin[0].toUpperCase() === word) {
            // 判断数组中有没有，没有就放
            // 放到cityList中
            if (this.indexList.indexOf(word) < 0) {
              this.indexList.push(word)
            }
            // this.cityList[word]?this.cityList[word].push(item):this.cityList[word]=[item]
            if (!this.cityList[word]) {
              this.cityList[word] = []
            }
            this.cityList[word].push(item)
          }
        })
        res.data.cities.forEach((item) => {
          if (item.isHot === 1) {
            if (this.hotcity.indexOf(item.name) < 0) {
              this.hotcity.push(item.name)
            }
          }
        })
      })
    })
  }
}
</script>

<style scoped lang='scss'>
.ab {
  width: 93%;
  background-color: #f4f4f4;
}
.top {
  height: 44px;
  line-height: 44px;
  width: 100%;
  display: flex;
  span {
    width: 100%;
    text-align: center;
  }
  i {
    font-size: 15px;
    position: absolute;
    left: 20px;
  }
}
.seach {
  width: 100%;
  height: 44px;
  background-color: #f4f4f4;
  input {
    width: 90%;
    height: 26px;
    outline: none;
    border: none;
    border-radius: 999em;
    font-size: 14px;
    text-indent: 3em;
    margin-top: 9px;
    margin-left: 5%;
  }
  i {
    font-size: 14px;
    position: absolute;
    left: 40px;
    top: 60px;
    color: #ccc;
  }
}
.hot {
  width: 100%;
  height: 130px;
  .hotCity {
    font-size: 13px;
    padding: 10px 20px;
  }
  .ccc {
    font-size: 13px;
    width: 80px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background-color: #f4f4f4;
    margin: 5px 20px;
    float: left;
  }
}
</style>
